package com.zretc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.zretc.dao.FansDao;
import com.zretc.entity.Account;
import com.zretc.entity.Fans;
import com.zretc.util.DBUtil;
import com.zretc.util.PageInfo;

/**
 * 粉丝功能dao实现类
 * @author Administrator
 *
 */

public class FansDaoImpl implements FansDao {

	@Override
	public int insertFans(Integer accountId, Integer fansId) {
		int n = DBUtil.doUpdate("insert into fans values(?,?)", accountId,fansId);
		return n;
	}

	@Override
	public Fans findFans(Integer accountId, Integer fansId) {
		Fans fans = null;
		ResultSet rs = DBUtil.doQuery("select account_id,fans_id from fans where account_id= ? and fans_id = ?", accountId,fansId);
		
		try {
			while (rs.next()) {
				fans = new Fans(accountId, fansId);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return fans;
	}

	@Override
	public int deleteFans(Integer accountId, Integer fansId) {
		int n = DBUtil.doUpdate("delete from fans where account_id= ? and fans_id = ?", accountId,fansId);
		return n;
	}

	@Override
	public int fansCount(Integer accountId) {
		int n = 0;
		ResultSet rs = DBUtil.doQuery("select count(*),account_id,fans_id from fans where account_id = ?", accountId);
		
		try {
			while (rs.next()) {
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return n;
	}

	@Override
	public PageInfo<Fans> findAccountByFansId(Map<String, String> map) {
		PageInfo<Fans> pageInfo = null;
		Fans fans = null;
		Account account = null;
		List<Fans> data = new ArrayList<Fans>();
		Integer fansId = Integer.parseInt(map.get("fansId"));
		Integer pageNum = Integer.parseInt(map.get("pageNum"));
		Integer pageSize = Integer.parseInt(map.get("pageSize"));
		ResultSet rs = DBUtil.doQuery("select B.account_id,A.fans_id,B.account_tel,B.account_picture,B.vip_status from"
						+ " fans A left join account B on A.account_id = B.account_id where A.fans_id = ? limit ?,?",fansId,(pageNum-1)*pageSize,pageSize);
		
		try {
			while (rs.next()) {
				Integer accountId = rs.getInt("account_id");
				String accountTel = rs.getString("account_tel");
				String accountPicture = rs.getString("account_picture");
				Integer vipStatus = rs.getInt("vip_status");
				account = new Account(accountId, accountTel, null, vipStatus, accountPicture);
				fans = new Fans(account, fansId);
				data.add(fans);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		Integer total = 0;
		
		rs = DBUtil.doQuery("select count(*),B.account_id,A.fans_id,B.account_tel,B.account_picture,B.vip_status from"
						+ " fans A left join account B on A.account_id = B.account_id where A.fans_id = ?", fansId);
	
		try {
			while (rs.next()) {
				total = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		pageInfo = new PageInfo<>(data, pageNum, pageSize, total);
		return pageInfo;
	}

	@Override
	public PageInfo<Fans> findFansByAccountId(Map<String, String> map) {
		PageInfo<Fans> pageInfo = null;
		Fans fans = null;
		Account fansAccount = null;
		List<Fans> data = new ArrayList<Fans>();
		Integer accountId = Integer.parseInt(map.get("accountId"));
		Integer pageNum = Integer.parseInt(map.get("pageNum"));
		Integer pageSize = Integer.parseInt(map.get("pageSize"));
		ResultSet rs = DBUtil.doQuery("select B.account_id,A.fans_id,B.account_tel,B.account_picture,B.vip_status from"
						+ " fans A left join account B on A.account_id = B.account_id where B.account_id = ? limit ?,?",accountId,(pageNum-1)*pageSize,pageSize);
		
		try {
			while (rs.next()) {
				Integer fansId = rs.getInt("fans_id");
				String accountTel = rs.getString("account_tel");
				String accountPicture = rs.getString("account_picture");
				Integer vipStatus = rs.getInt("vip_status");
				fansAccount = new Account(fansId, accountTel, null, vipStatus, accountPicture);
				fans = new Fans(fansAccount, accountId);
				data.add(fans);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		Integer total = 0;
		
		rs = DBUtil.doQuery("select count(*),B.account_id,A.fans_id,B.account_tel,B.account_picture,B.vip_status from"
						+ " fans A left join account B on A.account_id = B.account_id where B.account_id = ?", accountId);
	
		try {
			while (rs.next()) {
				total = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		pageInfo = new PageInfo<>(data, pageNum, pageSize, total);
		return pageInfo;
	}
	
}
